package com.study.jyg.rxjava2study.activity.txplayer;

import android.content.Context;
import android.os.Bundle;
import androidx.annotation.NonNull;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.recyclerview.widget.RecyclerView;

import com.study.jyg.rxjava2study.R;
import com.tencent.liteav.demo.superplayer.SuperPlayerModel;
import com.tencent.rtmp.ITXVodPlayListener;
import com.tencent.rtmp.TXLiveConstants;
import com.tencent.rtmp.TXVodPlayConfig;
import com.tencent.rtmp.TXVodPlayer;
import com.tencent.rtmp.ui.TXCloudVideoView;

import java.util.List;

public class ListPlayerAdapter extends RecyclerView.Adapter<ListPlayerAdapter.MyViewHolder> {

    private Context mContext;
    private List<String> list;

    public ListPlayerAdapter(Context mContext, List<String> list) {
        this.mContext = mContext;
        this.list = list;
    }

    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_list_player, viewGroup, false);
        return new MyViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull MyViewHolder myViewHolder, int i) {

    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    @Override
    public void onViewAttachedToWindow(@NonNull MyViewHolder holder) {
        super.onViewAttachedToWindow(holder);
        SuperPlayerModel model = new SuperPlayerModel();
        model.url = list.get(holder.getAdapterPosition());
        holder.mPlayer.startPlay(model.url);
        Log.e("ListPlayerAdapter", "Attached position = " + holder.getAdapterPosition());
    }

    @Override
    public void onViewDetachedFromWindow(@NonNull MyViewHolder holder) {
        super.onViewDetachedFromWindow(holder);
        holder.mPlayer.stopPlay(false);
        Log.e("ListPlayerAdapter", "Detached position = " + holder.getAdapterPosition());
    }

    static class MyViewHolder extends RecyclerView.ViewHolder {

        TXCloudVideoView superPlayerView;
        TXVodPlayer mPlayer;

        public MyViewHolder(@NonNull View itemView) {
            super(itemView);
            superPlayerView = itemView.findViewById(R.id.superPlayerView);
            superPlayerView.setRenderMode(TXLiveConstants.RENDER_MODE_FULL_FILL_SCREEN);
            mPlayer = new TXVodPlayer(itemView.getContext());
            TXVodPlayConfig config = new TXVodPlayConfig();
            config.setCacheFolderPath(itemView.getContext().getCacheDir().getAbsolutePath());
            config.setMaxCacheItems(15);
            mPlayer.setConfig(config);
            mPlayer.setAutoPlay(true);
            mPlayer.setVodListener(new ITXVodPlayListener() {
                @Override
                public void onPlayEvent(TXVodPlayer txVodPlayer, int i, Bundle bundle) {
                    switch (i) {
                        case TXLiveConstants.PLAY_EVT_PLAY_BEGIN://加载完成，开始播放的回调
                            Log.e("index", "加载完成，开始播放的回调");
//                            mStartPlay = true;
//                            if (mActionListener != null) {
//                                mActionListener.onPlayBegin();
//                            }
                            break;
                        case TXLiveConstants.PLAY_EVT_PLAY_LOADING: //开始加载的回调
                            Log.e("index", "开始加载的回调");
//                            if (mActionListener != null) {
//                                mActionListener.onPlayLoading();
//                            }
                            break;
                        case TXLiveConstants.PLAY_EVT_PLAY_END://获取到视频播放完毕的回调
                            Log.e("index", "获取到视频播放完毕的回调");
//                            replay();
//                            if (!mEndPlay) {
//                                mEndPlay = true;
//                    if (mVideoBean != null) {
//                        HttpUtil.videoWatchEnd(mVideoBean.getUid(), mVideoBean.getId());
//                    }
//                            }
                            break;
                        case TXLiveConstants.PLAY_EVT_RCV_FIRST_I_FRAME://获取到视频首帧回调
                            Log.e("index", "获取到视频首帧回调");
//                            if (mActionListener != null) {
//                                mActionListener.onFirstFrame();
//                            }
                            break;
                        case TXLiveConstants.PLAY_EVT_CHANGE_RESOLUTION://获取到视频宽高回调
                            Log.e("index", "获取到视频宽高回调");
                            onVideoSizeChanged(bundle.getInt("EVT_PARAM1", 0), bundle.getInt("EVT_PARAM2", 0));
                            break;
                    }
                }

                @Override
                public void onNetStatus(TXVodPlayer txVodPlayer, Bundle bundle) {
//                    ToastUtils.show("onNetStatus");
                }
            });
//        mPlayer.setLoop(true);//开启循环播放
            mPlayer.setPlayerView(superPlayerView);
        }

        /**
         * 获取到视频宽高回调
         */
        public void onVideoSizeChanged(float videoWidth, float videoHeight) {
            if (superPlayerView != null && videoWidth > 0 && videoHeight > 0) {
                ConstraintLayout.LayoutParams params = (ConstraintLayout.LayoutParams) superPlayerView.getLayoutParams();
                int targetH = 0;
                if (videoWidth / videoHeight > 0.5625f) {//横屏 9:16=0.5625
                    targetH = (int) (superPlayerView.getWidth() / videoWidth * videoHeight);
                } else {
                    targetH = ViewGroup.LayoutParams.MATCH_PARENT;
                }
                if (targetH != params.height) {
                    params.height = targetH;
                    superPlayerView.requestLayout();
                }
//                if (mVideoCover != null && mVideoCover.getVisibility() == View.VISIBLE) {
//                    mVideoCover.setVisibility(View.INVISIBLE);
//                }
            }
        }
    }
}
